package com.zhiwang.planet.ui.main.v2

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexboxLayoutManager
import com.zhiwang.planet.R
import com.zhiwang.planet.databinding.FragmentMoneyListBinding
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.mvvm.viewmodel.ItemDecorationViewModel
import com.zhiwang.planet.mvvm.viewmodel.ListMoneyViewModel
import com.zhiwang.planet.ui.BaseFragment
import kotlinx.android.synthetic.main.fragment_money_list.*
import javax.inject.Inject

class ListFragment : BaseFragment() {

    companion object {

        fun newInstance(type: String): ListFragment {
            val args = Bundle()
            val fragment = ListFragment()
            args.putString("type", type)
            fragment.arguments = args
            return fragment
        }
    }


    @Inject
    lateinit var listViewModel: ListMoneyViewModel

    @Inject
    lateinit var decoVm : ItemDecorationViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        DaggerUserComponent.builder()
                .autoDisposeModule(AutoDisposeModule(this))
                .appComponent(getAppComponent())
                .accountModule(AccountModule(requireActivity() as AppCompatActivity))
                .build()
                .inject(this)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val binding = DataBindingUtil.inflate<FragmentMoneyListBinding>(
                inflater,
                R.layout.fragment_money_list,
                container,
                false)
        binding.vm = listViewModel
        binding.decoration = decoVm
        binding.lifecycleOwner = viewLifecycleOwner
        listViewModel.type = requireArguments().getString("type", "")

        return binding.root

    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        listViewModel.load()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)

        if (hidden.not()) {
            if (::listViewModel.isLateinit)
                listViewModel.load()
        }
    }

}
